package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.come.Frame.AnswerMinJframe;
import org.come.Frame.ZhuFrame;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.DataUtil;
import org.come.until.FormsManagement;
import org.come.until.Util;

import com.tool.tcpimg.UIUtils;

import come.tool.utilEnum.AnswerEnum;

/**
 * 种树
 * 
 * @author admin
 *
 */
public class AnswerMinJpanel extends JPanel {

	private String text = "床前明月光，疑是地上霜。这首诗的作者在创作时候身高多少？";
	
	private JLabel d1,d2,d3,d4;
	
	public static String[] da = {"A：0.5米","B：3米","C：不知道","D：问老G"};
	
	public static String answer = "";
	private int currentChar = 0;
	private Timer timer; // 定时器变量
	
	public static String NPCID = "";
	
	public AnswerMinJpanel() {

		this.setPreferredSize(new Dimension(495, 268));
		this.setLayout(null);
		this.setBackground(UIUtils.Color_BACK);

		Font font = new Font("楷体", 0, 16);

		d1 = new JLabel();
		d1.setForeground(Color.white);
		d1.setFont(font);
		d1.setText(da[0]);
		d1.setBounds(210, 120, 100, 20);
		d1.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {
                d1.setLocation(210, 120);
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				int x = d1.getX();
                int y = d1.getY();
                d1.setLocation(x + 2, y + 2);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				d1.setForeground(Color.WHITE);
                d1.setLocation(210, 120);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				d1.setForeground(Color.GREEN);
                d1.setLocation(210, 120);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
                d1.setLocation(210, 120);
                getDa(d1.getText());
			}
		});
		add(d1);
		
		d2 = new JLabel();
		d2.setForeground(Color.white);
		d2.setFont(font);
		d2.setText(da[1]);
		d2.setBounds(350, 120, 100, 20);
		d2.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				d2.setLocation(350, 120);
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				int x = d2.getX();
                int y = d2.getY();
                d2.setLocation(x + 2, y + 2);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				d2.setForeground(Color.WHITE);
				d2.setLocation(350, 120);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				d2.setForeground(Color.GREEN);
				d2.setLocation(350, 120);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				d2.setLocation(350, 120);
                getDa(d2.getText());
			}
		});
		add(d2);
		
		d3 = new JLabel();
		d3.setForeground(Color.white);
		d3.setFont(font);
		d3.setText(da[2]);
		d3.setBounds(210, 155, 100, 20);
		d3.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				d3.setLocation(210, 155);
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				int x = d3.getX();
                int y = d3.getY();
                d3.setLocation(x + 2, y + 2);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				d3.setForeground(Color.WHITE);
				d3.setLocation(210, 155);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				d3.setForeground(Color.GREEN);
				d3.setLocation(210, 155);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				d3.setLocation(210, 155);
                getDa(d3.getText());
			}
		});
		add(d3);
		
		d4 = new JLabel();
		d4.setForeground(Color.white);
		d4.setFont(font);
		d4.setText(da[3]);
		d4.setBounds(350, 155, 100, 20);
		d4.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				d4.setLocation(350, 155);
				// TODO Auto-generated method stub
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				int x = d4.getX();
                int y = d4.getY();
                d4.setLocation(x + 2, y + 2);
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				d4.setForeground(Color.WHITE);
				d4.setLocation(350, 155);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				d4.setForeground(Color.GREEN);
				d4.setLocation(350, 155);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				d4.setLocation(350, 155);
                getDa(d4.getText());
			}
		});
		add(d4);
	}

	public void initTimer() {
//		text = "床前明月光，疑是地上霜。这首诗的作者在创作时候身高多少？";
		if (timer != null && timer.isRunning()) {
			timer.stop(); // 如果定时器正在运行，先停止
		}
		currentChar = 0; // 重置当前字符计数
		timer = new Timer(100, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				currentChar++;
				if (currentChar >= text.length()) {
					currentChar = text.length();
					timer.stop(); // 停止定时器
				}
				repaint(); // 重绘面板
			}
		});
		timer.start(); // 启动定时器
	}
	
	public ImageIcon icon1 = CutButtonImage.getImage("inkImg/hongmu/答题001.png", 495, 268);

	@Override
	protected void paintComponent(Graphics g) {
	    super.paintComponent(g);
	    g.drawImage(icon1.getImage(), 0, 0, 495, 268, this);
	    String displayedText = text.substring(0, currentChar);
	    int breakIndex = 13; // 设置换行的位置
	    String firstLine, secondLine;

	    if (currentChar > breakIndex) {
	        firstLine = displayedText.substring(0, breakIndex);
	        secondLine = displayedText.substring(breakIndex); // 剩下的字符
	    } else {
	        firstLine = displayedText;
	        secondLine = "";
	    }

	    g.setColor(Color.YELLOW);
	    g.setFont(UIUtils.TEXT_FONT_17);
	    
	    // 显示第一行
	    g.drawString(firstLine, 205, 80); // 调整文本的显示位置
	    // 显示第二行（如果有的话）
	    if (!secondLine.isEmpty()) {
	        g.drawString(secondLine, 205, 100); // 调整第二行的显示位置
	    }
	    if(currentChar >= text.length()) {
	    	d1.setVisible(true);
	    	d2.setVisible(true);
	    	d3.setVisible(true);
	    	d4.setVisible(true);
	    }else {
	    	d1.setVisible(false);
	    	d2.setVisible(false);
	    	d3.setVisible(false);
	    	d4.setVisible(false);
	    }
	}
	
	
	
	public static void getDa(String string) {
		if(DataUtil.isNotEmpty(AnswerMinJframe.getAnswerMinJpanel().NPCID)) {
			if(answer.equals(string)) {
				ZhuFrame.getZhuJpanel().addPrompt("#R恭喜你：#W回答正确！");
				FormsManagement.HideForm(3075);
				SendMessageUntil.toServer(Agreement.getAgreement().userAgreement("我来沾沾福气="+AnswerMinJframe.getAnswerMinJpanel().NPCID));
				AnswerMinJframe.getAnswerMinJpanel().NPCID = "";
			}else {
				ZhuFrame.getZhuJpanel().addPrompt("#W确定选这个吗？要不在选其他的试试？");
//				FormsManagement.HideForm(3075);
			}
		}
	}

	public void getWt() {
		int value = Util.random.nextInt(6);
		text = AnswerEnum.getTopic(value);
		da = AnswerEnum.getOption(value).split(",");
		answer = AnswerEnum.getAnswer(value);
		d1.setText(da[0]);
		d2.setText(da[1]);
		d3.setText(da[2]);
		d4.setText(da[3]);
	}
}
